package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.domain.MediaDomain;
import com.edais.service.MediaService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;
@Controller
@RequestMapping("/media")

public class MediaAction implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2668685103600942080L;
	private static final Log logger = LogFactory.getLog(MediaAction.class);
	@Autowired
	private MediaService mediaService;
	@RequestMapping(value = "/getMedia")
	public void getMedia(HttpServletRequest request, HttpServletResponse response){
		Integer current = 1;
		Integer pageSize = 10;//默认显示10条
		
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String currentPageStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);//当前页
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);//页大小
		String sign = paramsMap.get("sign");
		Map<String, Object> map = new HashMap<String, Object>();
		
		//分页参数
		if (currentPageStr != null && !("".equals(currentPageStr))) {
			current = Integer.parseInt(currentPageStr);
		}
		if (pageSizeStr != null && !("".equals(pageSizeStr))) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		
		map.put("rowBegin", (current-1)*pageSize);
		map.put("rowEnd",pageSize);
		map.put("sign", sign);
		try {	
		/*String userId = paramsMap.get("userId");   // 用户id

		paramMap.put("rowBegin", "0");
		paramMap.put("rowEnd","10");
		paramMap.put("sign", sign);*/
		String result = mediaService.getMediaNo(map);
		
		
		// 处理返回结果
					CommonUtil.responseJson(result, response);
					
		} catch (Exception e) {
					e.printStackTrace();
					logger.debug("/media/getMedia 媒体 异常：" + e.getCause());
					String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(),Consts.ERROR_CODE,
						Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
					CommonUtil.responseJson(result, response);
		}
		
	
	}
	
	@RequestMapping(value = "/getMediaContent")
	public void getMediaContent(HttpServletRequest request, HttpServletResponse response){
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String noticeId = paramsMap.get("noticeId");
		
		//Map<String, Object> map = new HashMap<String, Object>();		
		//map.put("noticeId", noticeId);
		
		try {	
			/*String userId = paramsMap.get("userId");   // 用户id

			paramMap.put("rowBegin", "0");
			paramMap.put("rowEnd","10");
			paramMap.put("sign", sign);*/
			MediaDomain mediaDomain = mediaService.getMediaContent(noticeId);
			
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("id", mediaDomain.getId());
			jsonMap.put("title", mediaDomain.getTitle());
			jsonMap.put("afficheContent", mediaDomain.getAfficheContent());
			jsonMap.put("createPerson", mediaDomain.getCreatePerson());
			jsonMap.put("createTime", mediaDomain.getCreateTime());
			jsonMap.put("createPersonNam", mediaDomain.getCreatePersonNam());
			jsonMap.put("intro", mediaDomain.getIntro());
			jsonMap.put("smallIconUrl", mediaDomain.getSmallIconUrl());
			jsonMap.put("htmlUrl", mediaDomain.getHtmlUrl());
			// 处理返回结果
			
			jsonMap.put("rescode", Consts.SUCCESS_CODE);
			jsonMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			jsonMap.put("resmsg_cn", "");
			/** 处理返回结果 */
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
						CommonUtil.responseJson(result, response);
						
			} catch (Exception e) {
						e.printStackTrace();
						logger.debug("/media/getMedia 媒体 异常：" + e.getCause());
						String result = CommonUtil.setResultStringCn(
							new HashMap<String, Object>(),Consts.ERROR_CODE,
							Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
						CommonUtil.responseJson(result, response);
			}
	}
}
